package com.mobile.check.activitys;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.kit.NToast;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import com.mobile.check.app.R;
import com.mobile.check.present.BasePresent;
import com.mobile.check.util.AppContents;
import com.mobile.check.views.MyEditText;

/**
 * 类名: InputEditActivity
 * 作者: 李学伟
 * 日期: 2020年10月14日 18:37
 * 描述:
 */
public class SettingActivity extends XActivity<BasePresent> {
    @BindView(R.id.userTv)
    MyEditText userTv;
    @BindView(R.id.deviceTv)
    MyEditText deviceTv;
    int type;
    @BindView(R.id.waitTimeTv)
    MyEditText waitTimeTv;
    @BindView(R.id.yesRbtn)
    RadioButton yesRbtn;
    @BindView(R.id.noRbtn)
    RadioButton noRbtn;
    @BindView(R.id.timePassEt)
    MyEditText timePassEt;
    @BindView(R.id.pojiePassEt)
    MyEditText pojiePassEt;

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("设置");
        String userName = SharedPref.getInstance(this).getString(AppContents.USERNAME, "");
        String device = SharedPref.getInstance(this).getString(AppContents.TESTDEVICE, "");
        userTv.setValue(userName);
        deviceTv.setValue(device);

        int waitTime = SharedPref.getInstance(this).getInt(AppContents.WAITTIME, 10);
        waitTimeTv.setValue(String.valueOf(waitTime));

        boolean show = SharedPref.getInstance(this).getBoolean(AppContents.SHOWPOJIE, false);
        yesRbtn.setChecked(show);
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    public void getIntentData() {
        type = getIntent().getIntExtra("type", 0);
    }

    @Override
    public BasePresent newP() {
        return new BasePresent();
    }


    public static void lunch(Activity act) {
        Intent intent = new Intent(act, SettingActivity.class);
        act.startActivity(intent);
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    @OnClick({R.id.saveUserBtn, R.id.saveWaitTimeBtn, R.id.saveShowPojieBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.saveUserBtn:
                String user = userTv.getValue();
                String testDevice = deviceTv.getValue();
                SharedPref.getInstance(this).putString(AppContents.USERNAME, user);
                SharedPref.getInstance(this).putString(AppContents.TESTDEVICE, testDevice);
                setResult(Activity.RESULT_OK);
                finish();
                break;
            case R.id.saveWaitTimeBtn:
                String pass = timePassEt.getValue();
                if (!TextUtils.equals(pass,"123456789.")&&!AppContents.ISTEST) {
                    NToast.shortToast("密码错误");
                    break;
                }
                String time = waitTimeTv.getValue();
                int waitTime = 0;
                try {
                    waitTime = Integer.parseInt(time);
                } catch (NumberFormatException e) {
                    NToast.shortToast("请输入整数时间");
                    break;
                }
                SharedPref.getInstance(this).putInt(AppContents.WAITTIME, waitTime);
                setResult(Activity.RESULT_OK);
                finish();
                break;
            case R.id.saveShowPojieBtn:
                String pass2 = pojiePassEt.getValue();
                if (!TextUtils.equals(pass2,"123456789.")&&!AppContents.ISTEST) {
                    NToast.shortToast("密码错误");
                    break;
                }
                if (yesRbtn.isChecked()) {
                    SharedPref.getInstance(this).putBoolean(AppContents.SHOWPOJIE, true);
                } else {
                    SharedPref.getInstance(this).putBoolean(AppContents.SHOWPOJIE, false);
                }
                setResult(Activity.RESULT_OK);
                finish();
                break;
        }
    }

}
